﻿using IMES.Common.Domain.KeepModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Keep.API.Appliction.Command.WarehouseFile
{
    /// <summary>
    /// 仓库列表命令处理器
    /// </summary>
    public class WarehouseListHandler : IRequestHandler<WarehouseListCommon, Result>
    {
        private readonly IBaseRepositorys<WarehouseManagement> _warehouseManagementRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="warehouseManagementRep"></param>
        public WarehouseListHandler(IBaseRepositorys<WarehouseManagement> warehouseManagementRep)
        {
            _warehouseManagementRep = warehouseManagementRep;
        }
        /// <summary>
        /// 处理仓库列表命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<Result> Handle(WarehouseListCommon request, CancellationToken cancellationToken)
        {
             var result = new Result() { Code = (int)CodeEnum.success, Messege = "获取仓库列表成功" };
            try
            {   
                var list = _warehouseManagementRep.GetAll().ToList();
                if(!string.IsNullOrEmpty(request.WarehouseName))
                {
                    list = list.Where(a => a.WarehouseName.Contains(request.WarehouseName)).ToList();
                }
                if (list.Count == 0)
                {
                    result.Code = (int)CodeEnum.search;
                    result.Messege = "暂无数据";
                }
                else
                {
                    result.Data = list;
                }

            }
            catch (Exception ex)
            {
                result.Code = (int)CodeEnum.error;
                result.Messege = ex.Message;
            }
            //// 返回结果
            return Task.FromResult(result);
         
        }
    }
}
